package com.richman.hg;

import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import com.richman.controller.DecreseRatio;
import com.richman.ps38.PS38Http;
import com.richman.ps38.entity.HGEvent;
import com.richman.ps38.entity.SendData;

public class HGManager {
	
	public static Vector<HGEvent> HGEvents = new Vector<HGEvent>();
	
	public static String hgUpdateStr = "";
	//public static ReadWriteLock HGEventsLock = new ReentrantReadWriteLock();
	
	public Vector<HGHttp> accounts = new Vector<HGHttp>();
	
	private static  ReadWriteLock hgeventsLock = new ReentrantReadWriteLock(); 
	
	public static Vector<HGEvent> gethgevents() {
		Vector<HGEvent> vec = new Vector<HGEvent>();
		hgeventsLock.readLock().lock();
		for(int i = 0; i < HGEvents.size(); i++){
			vec.add(HGEvents.elementAt(i));
		}		
		hgeventsLock.readLock().unlock();
		return vec;
	}
	
	public void init() {
		
		Vector<String[]> tmps = DecreseRatio.mainWnd.getHGAccounts();
		
		for(int i = 0; i <tmps.size(); i++ ) {
			
			HGHttp hghttp = new HGHttp();
			hghttp.setLoginParams(tmps.elementAt(i)[0], tmps.elementAt(i)[1],
					tmps.elementAt(i)[2]);
			
			

			accounts.add(hghttp);
			
		}
	}
	
	
	public void login() {
		
		
		
		
		
		try {
			
			for(int i = 0; i <accounts.size(); i++ ) {
				
				if(accounts.elementAt(i).getStatusCode() == -1) {
					accounts.elementAt(i).login();
					Thread.sleep(300);
				}
				
				if((accounts.elementAt(i).getStatusCode() == -2) && (System.currentTimeMillis() > accounts.elementAt(i).getCanLoginTime())) {
					accounts.elementAt(i).login();
					Thread.sleep(300);
				}

			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}

	}
	
	public void grabEvents() {
		
		
		
		try {
			
			SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");// 设置日期格式
			
			String showText = "";
			
			String successText = "";
			String failText = "";
			String notUsefulText = "";
			
			int second = DecreseRatio.mainWnd.getUpdateSeconds();
			
			
			
			int usefulAccountNum = 0;
			
			for(int i = 0; i <accounts.size(); i++ ) {
				
				if(accounts.elementAt(i).getStatusCode() == 1 || accounts.elementAt(i).getStatusCode() == 2 || accounts.elementAt(i).getStatusCode() == 3) {
					usefulAccountNum++;
				}
			}
			
			
			
			if(usefulAccountNum*second < 3000 && usefulAccountNum != 0) {
				second = 3000/usefulAccountNum;
			}
			
			int millis = second;
			

			String failstr = " 登录中";
			if(System.currentTimeMillis() - DecreseRatio.startEngineTime > 50*1000) {
				failstr = " 无法获取赛事列表";
			}
			
			
			
			for(int i = 0; i <accounts.size(); i++ ) {

				HGManager.HGEvents.clear();
				
				
				if(accounts.elementAt(i).getStatusCode() == 1 || accounts.elementAt(i).getStatusCode() == 2 || accounts.elementAt(i).getStatusCode() == 3) {
					boolean res1 = accounts.elementAt(i).getInplaybet();
					boolean res2 = accounts.elementAt(i).getTotalBet();
					
					if(res1&&res2) {
						//DecreseRatio.mainWnd.updateHGEventsTable(HGManager.HGEvents);
						DecreseRatio.mainWnd.updateTeamMatchHGTeams(HGManager.HGEvents);						
						DecreseRatio.isnps38EventManager.updatehgevents(HGManager.HGEvents);		
						accounts.elementAt(i).updateStatusCode(2);
					}
				}
				
				if(accounts.elementAt(i).getStatusCode() == 2) {
					successText += "  " +accounts.elementAt(i).getAccount() + "  " + df.format(System.currentTimeMillis()) + " "; 
				}else{
					failText += "  " +accounts.elementAt(i).getAccount() + "  " + df.format(System.currentTimeMillis()) + " "; 
				}
				
				if(!successText.equals("")) {
					showText =  successText + " 获取赛事列表成功" + "\n";
				}
				
				if(!failText.equals("")) {
					showText += failText + failstr + "\n";
				}
				
				
				hgUpdateStr = showText;
				
				
				DecreseRatio.mainWnd.updateHGTextArea(showText);
				
				
				DecreseRatio.mainWnd.updateHGAccountStatus(accounts.elementAt(i).getAccount(), accounts.elementAt(i).getStatusCode());
				
				//DaShui.mainWnd.updateISNAccountStatus(accounts.elementAt(i).getAccount(), accounts.elementAt(i).getStatusCode());

				Thread.sleep(millis);

			}
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		

		
		
		
	}
	

	
}
